package com.tjp.eight.ui.fragment.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.base.BasePresenterImpl;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.views.HackyViewPager;
import com.tjp.eight.views.HackysViewPager;
import com.tjp.eight.views.photoview.PhotoView;
import com.tjp.eight.views.photoviews.PhotoViewAttacher;
import com.tjp.eight.views.photoviews.PhotoViews;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/5/4.
 */
public class FragmentImageView extends BaseFragment {
    @Override
    protected BasePresenterImpl createPresenter() {
        return null;
    }

    TextView tv_current_pos;
    HackysViewPager viewPager;
    ImageModel imageModel;
    RelativeLayout rl_return;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);

        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_image, container, false);
        }

        initViews();

        return contentView;
    }

    void initViews() {
        rl_return = (RelativeLayout) contentView.findViewById(R.id.rl_return);
        rl_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(getActivity());
            }
        });
        String json = getActivity().getIntent().getStringExtra("json");
        imageModel = new Gson().fromJson(json, ImageModel.class);

        tv_current_pos = (TextView) contentView.findViewById(R.id.tv_current_pos);
        viewPager = (HackysViewPager) contentView.findViewById(R.id.rv_image);
        viewPager.setLocked(false);
        ArrayList<String> urls = new ArrayList<>();
        for (String url : imageModel.src) {
            urls.add(url);
        }
        final ImageAdapter adapter = new ImageAdapter(urls);
        final int count = urls.size();
        final int cpos = imageModel.postion + 1;
        tv_current_pos.setText(cpos + "/" + count);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(imageModel.postion);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int pos = position + 1;
                tv_current_pos.setText(pos + "/" + count);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class ImageAdapter extends AbstractViewPagerAdapter<String> {

        public ImageAdapter(ArrayList<String> sr) {
            super(sr);
        }

        @Override
        public View newView(int position) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.image_zoom, viewPager, false);
            PhotoViews image_zoom = (PhotoViews) view.findViewById(R.id.image_zoom);
            Glide.with(getContext()).load(getItem(position)).placeholder(R.drawable.loading).dontAnimate().into(new GlideDrawableImageViewTarget(image_zoom, -1));
            return view;
        }
    }
}
